package com.romadan.gui.controller.impl;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import com.romadan.core.linearanalys.fet.model14els.ExtrinsicElements;
import com.romadan.core.linearanalys.fet.model14els.IntrinsicElements;
import com.romadan.core.linearanalys.logic.LParameters;
import com.romadan.general.settings.Diagram;
import com.romadan.gui.controller.AbstractController;
import com.romadan.gui.utils.IntrinsicElementTableRow;

public class ResultLinearAnalysisTabController extends AbstractController
		implements Initializable {

	public void initialize(URL url, ResourceBundle resBundle) {
		resultTab = this;

		cbIntElement.getItems().clear();
		// Initialize cell factory for table of intrinsic parameters
		tcVgs.setCellValueFactory(new PropertyValueFactory<IntrinsicElementTableRow, String>(
				"vgs"));
		tcVds.setCellValueFactory(new PropertyValueFactory<IntrinsicElementTableRow, String>(
				"vds"));
		tcValue.setCellValueFactory(new PropertyValueFactory<IntrinsicElementTableRow, String>(
				"value"));
	}

	public void initResults() {
		lvExtElements.getItems().clear();
		ExtrinsicElements extElements = lParameters.get(0).getExtElements();
		for (String eName : extElements.getAllElements().keySet()) {
			String item = eName + " = " + Math.abs(extElements.getAllElements().get(eName));
			lvExtElements.getItems().add(item);
		}
		
		cbIntElement.getItems().clear();
		IntrinsicElements intElements = lParameters.get(0).getIntElements();
		cbIntElement.getItems().addAll(intElements.getAllElements().keySet());
	}

	@FXML
	private void view2Plot() {
		openDiagram(Diagram.DIAGRAM2X);
	}

	@FXML
	private void view3Plot() {
		openDiagram(Diagram.DIAGRAM3X);
	}

	@FXML
	private void chooseIntElement() {
		if (prjState.isMultipleMode()) {
			btn2Plot.setDisable(false);
			btn3Plot.setDisable(false);
		}
		String elName = cbIntElement.getSelectionModel().getSelectedItem();
		if (elName != null && elName.length() != 0) {
			ObservableList<IntrinsicElementTableRow> dataTable = FXCollections
					.observableArrayList();
			for (LParameters l : lParameters) {
				IntrinsicElements intElements = l.getIntElements();
				Double elValue = intElements.getAllElements().get(elName);
				String value = elValue.toString();
				String vds = String.valueOf(l.getVoltageDS());
				String vgs = String.valueOf(l.getVoltageGS());

				dataTable.add(new IntrinsicElementTableRow(vgs, vds, value));
			}
			tbIntElements.setItems(dataTable);
		}
	}

	@FXML
	private ListView<String> lvExtElements;
	@FXML
	private TableView<IntrinsicElementTableRow> tbIntElements;
	@FXML
	private TableColumn<IntrinsicElementTableRow, String> tcVds;
	@FXML
	private TableColumn<IntrinsicElementTableRow, String> tcVgs;
	@FXML
	private TableColumn<IntrinsicElementTableRow, String> tcValue;
	@FXML
	private ComboBox<String> cbIntElement;
	@FXML
	private Button btn2Plot;
	@FXML
	private Button btn3Plot;
}
